package com.snow.order.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.snow.order.entity.Food;
import com.snow.order.entity.User;
import com.snow.order.log.Log;


public class UserDao extends BasicDao {

	{
		TABLE_NAME = "t_user";
	}

	/**
	 * 		登陆
	 * @param uname
	 * @param pwd
	 * @return
	 * @throws Exception
	 */
	public User login(String uname, String pwd) throws Exception {
		Log.logger.info(uname + "正在登陆...");
		System.out.println("uname" + uname + ",pwd" + pwd+TABLE_NAME);
		User user = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", uname);
		map.put("pwd", pwd);
		List<User> list = query(null, map, new IQuery() {

			public List<User> getListFromResultSet(ResultSet resultSet)
					throws SQLException {

				List<User> result = new ArrayList<User>();
				User user;
				while (resultSet.next()) {
					user = new User();
					user.setName(resultSet.getString("name"));
					user.setPwd(resultSet.getString("pwd"));
					user.setRole(resultSet.getString("role"));
					user.setNo(resultSet.getInt("no"));
					user.setAddress(resultSet.getString("address"));
					user.setTel(resultSet.getString("tel"));
					result.add(user);
				}
				return result;
			}

		});
		if (list.size() == 0) {
			Log.logger.info(uname + "锟斤拷录失锟斤拷");
		} else {
			user = list.get(0);
			Log.logger.info(uname + "锟斤拷录锟缴癸拷");

		}

		return user;

	}

	/**
	 * 		根据用户编号查个人信息，用于个人信息维护
	 * @param user
	 * @author 张圈圈
	 * @throws Exception
	 */
	public User getUser(String uno) throws Exception{
		Log.logger.info("正在获取个人信息...");
		Map<String, String> map = new HashMap<String, String>();
		map.put("no", uno);
		List<User> list = query(null, map, new IQuery() {
			public List<User> getListFromResultSet(ResultSet resultSet)
					throws SQLException {

				List<User> result = new ArrayList<User>();
				User user;
				while (resultSet.next()) {
					user = new User();
					user.setName(resultSet.getString("name"));
					user.setPwd(resultSet.getString("pwd"));
					user.setRole(resultSet.getString("role"));
					user.setAddress(resultSet.getString("address"));
					user.setTel(resultSet.getString("tel"));
					user.setTime(resultSet.getString("time"));
					result.add(user);
				}
				return result;
			}

		});
		System.out.println(list);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null ;
		}
	}

	/**
	 * 		注册
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public boolean register(User user) throws Exception {
		Log.logger.info(user.getName() + "正在注册...");

		String[] cols = {"no","name","pwd","address","tel","role","time"};
		String no = Integer.toString(this.getN0());
		String[] values = {no,user.getName(),user.getPwd(),user.getAddress(),
				user.getTel(),"b",user.getTime()} ;
		
		if(insert(cols, values) > 0){
			Log.logger.info(user.getName() + "注册成功...");
			return true ;
		}else{
			Log.logger.info(user.getName() + "注册失败...[insert(cols, values)失败]");
			return false ;
		}

	}
	
	/**
	 * 		修改用户信息操作
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public boolean updateUser(User user) throws Exception {
		Log.logger.info(user.getName() + "正在修改...");

		Map<String,String> set = new HashMap<String, String>();
		Map<String,String> where = new HashMap<String, String>();
		set.put("name",user.getName());
		set.put("pwd", user.getPwd());
		set.put("address", user.getAddress());
		set.put("tel",user.getTel());

		where.put("no", Integer.toString((user.getNo())));
		if(update(set,where)> 0){
			Log.logger.info(user.getName() + "修改成功...");
			return true ;
		}else{
			Log.logger.info(user.getName() + "修改失败...[update(cols, values)失败]");
			return false ;
		}

	}
	/**
	 * 判断用户名是否已存在，在注册时使用
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public boolean UserExist(String uname) throws ClassNotFoundException, SQLException {
		boolean flag = false;
		String sql = "select * from t_user  where name = '" + uname+"'";
		this.openconnection(); 
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		System.out.println(sql);
		if(rs.next()){
			flag = true ;
		}else{
			flag = false ;
		}
		return flag ;
	}
	
	public int getN0() throws Exception{
		int no = 0 ;
		String sql = "select max(no) from "+TABLE_NAME;
		this.openconnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		rs.next();
		no = rs.getInt(1)+1;
		return no ;
	}

	/**
	 * 查询所有用户
	 * @author 张佺佺
	 * @return
	 * @throws Exception
	 */
	public List<User> getAllUser() throws Exception {
		// TODO Auto-generated method stub
		List<User> userList = null;
		String sql = "select * from T_User";
		this.openconnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if (rs != null) {
			userList = new ArrayList<User>();
			while (rs.next()) {
				User user = new User();
				user.setNo(rs.getInt("NO"));
				user.setName(rs.getString("NAME"));
				user.setTel(rs.getString("TEl"));
				user.setRole(rs.getString("ROLE"));
				user.setTime(rs.getString("TIME"));
				userList.add(user);
			}
		}
		return userList;
	}
	
	/**
	 * 设为管理员
	 * @author 张佺佺
	 * @param no
	 * @throws Exception
	 */
	public void SetManager(int no,String role) throws Exception{
		String sql = "update T_USER set role = ? where no ="+no ;
		this.openconnection();
		PreparedStatement ps = conn.prepareStatement(sql) ;
		ps.setString(1, role);
		ps.executeUpdate();
	}
}
